package com.example.tvfoucssustom.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.tvfoucssustom.R;
import com.example.tvfoucssustom.TvCustomLayout;
import com.example.tvfoucssustom.base.BaseFragment;
import com.example.tvfoucssustom.data.Contans;
import com.example.tvfoucssustom.util.DebugLog;

import static android.view.KeyEvent.KEYCODE_DPAD_UP;
import static com.example.tvfoucssustom.data.Contans.HAND_FOUCS_DOWN;
import static com.example.tvfoucssustom.data.Contans.HAND_MEMORY_FOUCS_DEFULT;
import static com.example.tvfoucssustom.data.Contans.TAB_GESHOU_POSTION;
import static com.example.tvfoucssustom.data.Contans.TAB_MEMORY_FOUCS_DEFULT;
import static com.example.tvfoucssustom.data.Contans.TAB_MESSAGE_POSTION;
import static com.example.tvfoucssustom.data.Contans.TAB_MUSIC_POSTION;
import static com.example.tvfoucssustom.data.Contans.TAB_PHOTO_POSTION;
import static com.example.tvfoucssustom.data.Contans.TAB_SET_POSTION;
import static com.example.tvfoucssustom.data.Contans.TAB_VIDEO_POSTION;
import static com.example.tvfoucssustom.data.Contans.TAB_WOXIU_POSTION;
import static com.example.tvfoucssustom.data.Contans.TAB_YINYUE_POSTION;
import static com.example.tvfoucssustom.data.Contans.TAB_ZHUANJI_POSTION;
import static com.example.tvfoucssustom.util.DebugLog.LOG;

/**
 * Created by dasu on 2017/4/20.
 * <p>
 * 各类型干货的数据展示界面，
 */

public class PhotoTwoFragment extends BaseFragment implements TvCustomLayout.FoucsFragmentListener {
    private TvCustomLayout tvYinyue;
    private TvCustomLayout tvGeshou;
    private TvCustomLayout tvZhuanji;
    private Fragment currentFragment;
    private YinyueFragment yinyueFragment;
    private GeshouFragment geshouFragment;
    private ZhuanJiFragment zhuanJiFragment;

    public PhotoTwoFragment() {
    }

    private Context mContext;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
//        DebugLog.d(LOG+TAG + " ---- onAttach");

    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        DebugLog.d(LOG+TAG + " ---- onCreateView");
        View view = inflater.inflate(R.layout.acticity_fragment, container, false);
        initView(view);
        return view;
    }


    private void initView(View view) {
        tvYinyue = view.findViewById(R.id.yinyue);
        tvGeshou = view.findViewById(R.id.geshou);
        tvZhuanji = view.findViewById(R.id.zhuanji);
        initFragment();

        initEvent();
    }

    private void initFragment() {
        yinyueFragment = new YinyueFragment();
        geshouFragment = new GeshouFragment();
        zhuanJiFragment = new ZhuanJiFragment();
        //首次进来记忆
        selectFragment(yinyueFragment, 11);
        Contans.TAB_MEMORY_FOUCS = TAB_YINYUE_POSTION;
    }

    private void initEvent() {
        tvYinyue.setFoucsFragmentListener(this);
        tvGeshou.setFoucsFragmentListener(this);
        tvZhuanji.setFoucsFragmentListener(this);

    }

    private void selectFragment(Fragment fragment, int tag) {
        FragmentTransaction mFt = getActivity().getSupportFragmentManager().beginTransaction();
        if(currentFragment!=null){
            mFt.hide(currentFragment);
        }
        currentFragment = getActivity().getSupportFragmentManager().findFragmentByTag(tag + "");
        if (currentFragment == null) {
            currentFragment = fragment;
            mFt.add(R.id.home_tab_container2, currentFragment, String.valueOf(tag));
        } else {
            mFt.show(currentFragment);
        }
        mFt.commit();

    }


    @Override
    public void seleteFoucsTab(int tag) {
        DebugLog.d(DebugLog.LOG + "---seleteFoucsTab  " + tag);
        Contans.TAB_MEMORY_FOUCS = tag;
        switch (tag) {
            case 11:
                selectFragment(yinyueFragment, 11);
                break;
            case 12:

                selectFragment(geshouFragment, 12);
                break;
            case 13:
                selectFragment(zhuanJiFragment, 13);
                break;

        }
    }

    @Override
    public void unSeleteFoucsTab(int tag) {
        DebugLog.d(LOG + " ---- unSeleteFoucsTab--- " + tag);
        unSelectFragment();
    }

    @Override
    public void setActivatedTab(Integer tag) {
        //保持高亮的标题
        Contans.TAB_MEMORY_FOUCS = tag;
    }

    private void unSelectFragment() {
        if (Contans.TAB_SAVE_DEFULT == Contans.TAB_SAVE_FRAGMENT) {
            return;
        }
        if (currentFragment != null) {
            FragmentTransaction mFt = getActivity().getSupportFragmentManager().beginTransaction();
            mFt.hide(currentFragment);
            mFt.commit();
        }

    }

    @Override
    public void onStop() {
        super.onStop();
        Contans.TAB_MEMORY_FOUCS = TAB_MEMORY_FOUCS_DEFULT;
        Contans.HAND_MEMORY_FOUCS = HAND_MEMORY_FOUCS_DEFULT;
        Contans.TAB_SAVE_DEFULT = -1000;
    }


//    public void onKeyDownChild(int keyCode, KeyEvent event) {
//        DebugLog.d(DebugLog.LOG + "---keyCode----  " + keyCode + " Contans.TAB_MEMORY_FOUCS " + Contans.TAB_MEMORY_FOUCS);
//        switch (keyCode) {
//            case KEYCODE_DPAD_UP:
//                if (Contans.TAB_MEMORY_FOUCS == TAB_GESHOU_POSTION) {
//                    tvGeshou.requestFocus();
//                    Contans.HAND_MEMORY_FOUCS = HAND_MEMORY_FOUCS_DEFULT;
//                    return;
//                }
//                if (Contans.TAB_MEMORY_FOUCS == TAB_ZHUANJI_POSTION) {
//                    tvZhuanji.requestFocus();
//                    Contans.HAND_MEMORY_FOUCS = HAND_MEMORY_FOUCS_DEFULT;
//                    return;
//                }
//                if (Contans.TAB_MEMORY_FOUCS == TAB_YINYUE_POSTION) {
//                    DebugLog.d(DebugLog.LOG + " photoFragment.isBefore() " + yinyueFragment.isBefore());
//                    if (yinyueFragment.isBefore()) {
//
//                        tvYinyue.requestFocus();
//                        Contans.HAND_MEMORY_FOUCS = HAND_MEMORY_FOUCS_DEFULT;
//                        return;
//                    }
//                }
//
//                break;
//        }
//
//    }


    public void requestGehsou() {
        tvGeshou.requestFocus();
        Contans.HAND_MEMORY_FOUCS = HAND_MEMORY_FOUCS_DEFULT;
    }

    public boolean isGeshouFoucs() {
        return geshouFragment.isFoucs();
    }

    public boolean isGeshouTabFoucs() {
        return tvGeshou.hasFocus();
    }


    public boolean isYinyue() {
        return yinyueFragment.isBefore();
    }

    public void requestYinyue() {
        tvYinyue.requestFocus();
        Contans.HAND_MEMORY_FOUCS = HAND_MEMORY_FOUCS_DEFULT;
    }

    public boolean isYinyueTabFoucs() {
        return tvYinyue.hasFocus();
    }


    public void requestzhuanji() {
        tvZhuanji.requestFocus();
        Contans.HAND_MEMORY_FOUCS = HAND_MEMORY_FOUCS_DEFULT;
    }

    public boolean isZuanjiFoucs() {
        return zhuanJiFragment.isFoucs();
    }

    public boolean isZuanjiTabFoucs() {
        return tvZhuanji.hasFocus();
    }


    public void setSelect(int i) {

    }
}
